﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using Coding4Fun.Controls.Data;
using Expression.Blend.SampleData.PhoneType;

namespace Coding4Fun.FakeCall.Data
{
	[DataContract]
	public class FakeCallSettings : INotifyPropertyChanged
	{
		public static FakeCallSettings Instance { get; set; }

		static FakeCallSettings()
		{
			Instance = Load();
		}



		#region ShowedMediaWarning (INotifyPropertyChanged Property)
		private bool _showedMediaWarning;
		[DataMember]
		public bool ShowedMediaWarning
		{
			get { return _showedMediaWarning; }
			set
			{
				_showedMediaWarning = value;
				RaisePropertyChanged("ShowedMediaWarning");
			}
		}
		#endregion

		#region general screen
		private TimeSpan _timeDelay;
		[DataMember]
		public TimeSpan TimeDelay
		{
			get { return _timeDelay; }
			set
			{
				_timeDelay = value;
				RaisePropertyChanged("TimeDelay");
			}
		}

		private bool _customizeSettings;
		[DataMember]
		public bool CustomizeSettings
		{
			get { return _customizeSettings; }
			set
			{
				_customizeSettings = value;
				RaisePropertyChanged("CustomizeSettings");
			}
		}

		private bool _ringPhone;
		[DataMember]
		public bool RingPhone
		{
			get { return _ringPhone; }
			set
			{
				_ringPhone = value;
				RaisePropertyChanged("RingPhone");
			}
		}

		private bool _vibratePhone;
		[DataMember]
		public bool VibratePhone
		{
			get { return _vibratePhone; }
			set
			{
				_vibratePhone = value;
				RaisePropertyChanged("VibratePhone");
			}
		}

		private bool _expandTalkOptions;
		[DataMember]
		public bool ExpandTalkOptions
		{
			get { return _expandTalkOptions; }
			set
			{
				_expandTalkOptions = value;
				RaisePropertyChanged("ExpandTalkOptions");
			}
		}
		#endregion
		#region lock screen
		private DateTime _lockScreenDate;
		[DataMember]
		public DateTime LockScreenDate
		{
			get { return _lockScreenDate; }
			set
			{
				_lockScreenDate = value;
				RaisePropertyChanged("LockScreenDate");
			}
		}

		private DateTime _lockScreenTime;
		[DataMember]
		public DateTime LockScreenTime
		{
			get { return _lockScreenTime; }
			set
			{
				_lockScreenTime = value;
				RaisePropertyChanged("LockScreenTime");
			}
		}

		private DateTime _appointmentStartTime;
		[DataMember]
		public DateTime AppointmentStartTime
		{
			get { return _appointmentStartTime; }
			set
			{
				_appointmentStartTime = value;
				RaisePropertyChanged("AppointmentStartTime");
			}
		}

		private DateTime _appointmentEndTime;
		[DataMember]
		public DateTime AppointmentEndTime
		{
			get { return _appointmentEndTime; }
			set
			{
				_appointmentEndTime = value;
				RaisePropertyChanged("AppointmentEndTime");
			}
		}

		private string _appointmentName;
		[DataMember]
		public string AppointmentName
		{
			get { return _appointmentName; }
			set
			{
				_appointmentName = value;
				RaisePropertyChanged("AppointmentName");
			}
		}

		private string _appointmentLocation;
		[DataMember]
		public string AppointmentLocation
		{
			get { return _appointmentLocation; }
			set
			{
				_appointmentLocation = value;
				RaisePropertyChanged("AppointmentLocation");
			}
		}

		private string _lockScreenPhoto;
		[DataMember]
		public string LockScreenPhoto
		{
			get { return _lockScreenPhoto; }
			set
			{
				_lockScreenPhoto = value;
				RaisePropertyChanged("LockScreenPhoto");
			}
		}
		#endregion

		#region person

		private string _personPhoto;
		[DataMember]
		public string PersonPhoto
		{
			get { return _personPhoto; }
			set
			{
				_personPhoto = value;
				RaisePropertyChanged("PersonPhoto");
			}
		}

		private string _personName;
		[DataMember]
		public string PersonName
		{
			get { return _personName; }
			set
			{
				_personName = value;
				RaisePropertyChanged("PersonName");
			}
		}

		private string _personPhoneNumber;
		[DataMember]
		public string PersonPhoneNumber
		{
			get { return _personPhoneNumber; }
			set
			{
				if (value != null)
				{
					var temp = value.Replace("(", "").Replace(")", "").Replace(" ", "").Replace("-", "");

					double attemptValue;
					// 1 200-000-0000 is first valid North american number
					// 2 000 000 0000 is last
					if (double.TryParse(temp, out attemptValue) &&
					    attemptValue > 2000000000 &&
					    attemptValue < 20000000000)
					{
						value = attemptValue.FormatedPhoneNumber();
					}
				}
				_personPhoneNumber = value;
				RaisePropertyChanged("PersonPhoneNumber");
			}
		}

		private Item _phoneType;
		[DataMember]
		public Item PhoneType
		{
			get { return _phoneType; }
			set
			{
				_phoneType = value;
				RaisePropertyChanged("PhoneType");
			}
		}
		#endregion


		private const string SettingFileName = "settings.dat";
		public static FakeCallSettings Load()
		{
			var settings = (DesignerProperties.IsInDesignTool) ? new FakeCallSettings() : StorageHelper.LoadContract<FakeCallSettings>(SettingFileName, true);

			// load defaults
			if (settings.LockScreenDate == DateTime.MinValue)
				settings.LockScreenDate = DateTime.Now;

			if (settings.LockScreenTime == DateTime.MinValue)
				settings.LockScreenTime = DateTime.Now;

			if (settings.AppointmentStartTime == DateTime.MinValue)
			{
				settings.AppointmentStartTime = DateTime.Now;
				settings.AppointmentEndTime = DateTime.Now.AddMinutes(30);
			}

			if(settings.AppointmentStartTime > settings.AppointmentEndTime)
				settings.AppointmentEndTime = DateTime.Now.AddMinutes(30);


			return settings;
		}

		public void Save()
		{
			StorageHelper.SaveContract(SettingFileName, this, true);
		}

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		protected void RaisePropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion
	}
}